﻿Imports BUS
Imports DTO
Public Class UserControlTraCuuKhachHang
    Private khCrt As New KhachHangCrt()
    Private Sub chkTenKhachHang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTenKhachHang.CheckedChanged
        If chkTenKhachHang.Checked = True Then
            txtTenKhachHang.Enabled = True
        Else
            txtTenKhachHang.Enabled = False
        End If
    End Sub

    Private Sub chkCMND_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCMND.CheckedChanged
        If chkCMND.Checked = True Then
            txtCMND.Enabled = True
        Else
            txtCMND.Enabled = False
        End If
    End Sub

    Private Sub chkDiaChi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDiaChi.CheckedChanged
        If chkDiaChi.Checked = True Then
            txtDiaChi.Enabled = True
        Else
            txtDiaChi.Enabled = False
        End If
    End Sub

    Private Sub CapNhatTieuChuanTraCuu(ByVal bUpdated As Boolean)
        If (bUpdated) Then
            If (txtMaKhachHang.Text <> "") Then
                khCrt.MaKh = Integer.Parse(txtMaKhachHang.Text)
            Else
                khCrt.MaKh = 0
            End If
            khCrt.TenKH = txtTenKhachHang.Text
            khCrt.CMND = txtCMND.Text
            khCrt.DiaChi = txtDiaChi.Text

            khCrt.ChkTenKH = chkTenKhachHang.Checked
            khCrt.CMND = chkCMND.Checked
            khCrt.DiaChi = chkDiaChi.Checked
        End If
    End Sub

    Private Sub btnTraCuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTraCuu.Click
        Dim makh As Integer
        If txtMaKhachHang.Text.Length > 0 And (Integer.TryParse(txtMaKhachHang.Text, makh) = False Or makh <= 0) Then
            MessageBox.Show("Mã khách hàng không hợp lệ!")
            txtMaKhachHang.Focus()
            Return
        End If
        Dim dskh As New List(Of KhachHangCrt)()
        Try
            CapNhatTieuChuanTraCuu(True)
            Dim khBUS As New KhachHangBUS()
            dskh = khBUS.TimKiem(khCrt)
            If dskh.Count <= 0 Then
                MessageBox.Show("Không tìm thấy khách hàng!")
                Return
            End If
            Dim frmDS As New frmDanhSachTraCuuKhachHang()
            frmDS.DSKH = dskh
            frmDS.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
       
    End Sub
End Class
